package gitlet;

import org.junit.Before;
import org.junit.Test;// 这个导入是正确的

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Optional;

import static gitlet.Repository.GITLET_DIR;
import static gitlet.Repository.GITLET_DIR_OBJECT;
import static gitlet.Repository.GITLET_DIR_OBJECT_BLOBS;
import static gitlet.Utils.join;
import static org.junit.Assert.*;

public class BlobTest {

    public static boolean deleteDirectory(File directory) {
        if (!directory.exists()) {
            return false; // 如果文件夹不存在，返回 false
        }

        // 如果是目录，递归删除内部文件和子目录
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (File file : files) {
                    // 递归调用删除子文件或子目录
                    deleteDirectory(file);
                }
            }
        }

        // 删除当前空目录或文件
        return directory.delete();
    }

    @Before

    public void setup(){
       if(Repository.GITLET_DIR.exists()){
           deleteDirectory(Repository.GITLET_DIR);
       };
        GITLET_DIR.mkdir();
        GITLET_DIR_OBJECT.mkdir();
        GITLET_DIR_OBJECT_BLOBS.mkdir();
    }
    @Test
    public void testBlobCreation() {
        byte[] testData = "Hello, Blob!".getBytes();
        Blob blob = new Blob(testData);

        // 检查数据长度
        assertEquals(testData.length, blob.length());

        // 检查数据内容
        byte[] blobData = blob.getData();
        assertArrayEquals(testData, blobData);
    }

//    public void testFile() {
//        byte[] testData = "Hello, Blob!".getBytes();
//        Blob blob = new Blob(testData);
//        blob.saveBlob();
//    }

    @Test
    public void testSaveFile() {
        byte[] testData = "Hello, Blob!".getBytes();
        Blob blob = new Blob(testData);
        blob.saveBlob();
        String filePath = "965105a0d0583bce873d50b5f7971a37ae02e422";
        Blob a = Blob.fromFile(filePath);
        byte[] blobData = a.getData();
        a.saveBlob();
    }
}
